package com.hq.door.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.hq.door.bean.WeChatData;
import com.hq.door.bean.Button;
import com.hq.door.bean.WebWeChatData;
import com.hq.door.service.IWeChatService;
import com.hq.door.util.HttpClientUtil;
import com.hq.door.util.JacksonUtil;

@Service("WeChatService")
public class WeChatServiceImpl implements IWeChatService {

	@Autowired
	WeChatData weChatData;
	

//	@PostConstruct
//	@Scheduled(fixedDelay = 7000000)
	@Override
	public void getAccessToken() {
		ResourceBundle resource = ResourceBundle.getBundle("weChat-cfg");
		String appID = resource.getString("appID");
		String appsecret = resource.getString("appsecret");
		String accessUrl = resource.getString("accessUrl")+"&appid="
				+ appID + "&secret=" + appsecret;

		String result = HttpClientUtil.httpGetRequest(accessUrl);

		WeChatData Token = (WeChatData) JacksonUtil.json2object(result,
				WeChatData.class);

		if (Token.getErrcode() == null) {
			weChatData.setAccess_token(Token.getAccess_token());
			// System.out.println(accessToken.getAccess_token());
		} else {
			System.out.println("{errcode:" + Token.getErrcode() + " errmsg:"
					+ Token.getErrmsg() + "}");
		}
	}

//	@PostConstruct
	@Override
	public void createButton() {
		List<Button> button = new ArrayList<Button>();
		ResourceBundle resource = ResourceBundle.getBundle("weChat-cfg");

		String appID = resource.getString("appID");
		String appsecret = resource.getString("appsecret");
		String redirect_uri = resource.getString("redirect_uri");

		String menuUrl = resource.getString("menuUrl")+"?access_token=";
		String result;

		button.add(new Button());
		button.add(new Button());
		button.add(new Button());

		for (int i = 0; i < button.size(); i++) {
			button.get(i).setType(resource.getString("btntype"));
			button.get(i)
					.setName(resource.getString("btn" + (i + 1) + ".name"));
			try {
				button.get(i).setUrl(
						resource.getString("btnurl") + "?appid=" + appID
								+ "&redirect_uri=" + URLEncoder.encode(redirect_uri, "utf-8")
								+ "&response_type=code&scope=snsapi_base&state=" + i
								+ "#wechat_redirect");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		String jsonStr = JacksonUtil.object2Json(button);

		jsonStr = "{\"button\":" + jsonStr + "}";

		if (weChatData.getAccess_token() == null) {
			getAccessToken();
		}

		try {
			result = HttpClientUtil.httpPostRequest(
					menuUrl + weChatData.getAccess_token(), jsonStr);
			WeChatData res = (WeChatData) JacksonUtil.json2object(result,
					WeChatData.class);
			if (!"0".equals(res.getErrcode())) {
				System.out.println("{errcode:" + res.getErrcode() + " errmsg:"
						+ res.getErrmsg() + "}");
			} else {
				System.out.println("Button created succeed");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getWebAccessToken(String code) {
		WebWeChatData webWeChatData=null;
		
		ResourceBundle resource = ResourceBundle.getBundle("weChat-cfg");

		String appID = resource.getString("appID");
		String appsecret = resource.getString("appsecret");
		String url = resource.getString("oauth2url")+"?appid=";
		
		String result;
		
		result = HttpClientUtil.httpGetRequest(url+appID+"&secret="+appsecret+"&code="+code+"&grant_type=authorization_code");
		
		webWeChatData = (WebWeChatData) JacksonUtil.json2object(result, WebWeChatData.class);
		
		if(webWeChatData!=null&&webWeChatData.getOpenid()!=null){
			return webWeChatData.getOpenid();
		}else{
			System.out.println("{errcode:" + webWeChatData.getErrcode() + " errmsg:"
					+ webWeChatData.getErrmsg() + "}");
			return "";
		}

	}

}
